package org.ivwsqii.proxy.cglibDynamicProxy;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;


public class CglibProxyInterceptor implements MethodInterceptor{

	private Object target;

	public CglibProxyInterceptor(Object target){
		this.target = target;
	}
	public Object getInstance(){
		Enhancer enhancer = new Enhancer(); //创建加强器，用来创建动态代理类
		enhancer.setSuperclass(this.target.getClass());  //为加强器指定要代理的业务类（即：为下面生成的代理类指定父类）
		//设置回调：对于代理类上所有方法的调用，都会调用CallBack，而Callback则需要实现intercept()方法进行拦
		enhancer.setCallback(this);
		// 创建动态代理类对象并返回
		return enhancer.create();
	}

	@Override
	public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

		System.out.println("买房前");
		Object invoke = methodProxy.invokeSuper(o, objects);
		System.out.println("买房后");
		return invoke;
	}
}
